﻿using pbvs.Data.Repositories.Interfaces;
using pbvs.Models.DomainModels;
using pbvs.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using pbvs.Data.Repositories.RepositoryExtensions;
using pbvs.Services.ServiceExtensions;
using pbvs.Models;
using pbvs.Models.ViewModels;
using System.Web.Mvc;

namespace pbvs.Services
{
    public class ContentService : IContentService
    {
        private readonly IContentRepository _db;

        public ContentService(IContentRepository db)
        {
            _db = db;
        }

        public ContentPageDomainModel GetPage(string pageName)
        {
            return _db.Pages.ByPageName(pageName).FirstOrDefault().ToDomainModel();
        }

        public ContentPageDomainModel EditPage(ContentPageDomainModel page)
        {
            if (page.Id == 0)
            {
                page = AddContentPage(page);
            }
            else
            {
                if (_db.Pages.ById(page.Id).Any())
                {
                    page = UpdateContentPage(page);
                }
                else
                {
                    throw new Exception(string.Format("Content Page with Id == {0} not found", page.Id));
                }
            }
            return page;
        }

        public ContentPageViewModel LoadViewModel(int? id)
        {
            ContentPageViewModel vm = new ContentPageViewModel { pages = _db.Pages.ToDomainModels() };
            List<SelectListItem> contentPages = new List<SelectListItem>();
            foreach (ContentPageDomainModel pg in vm.pages)
            {
                SelectListItem item = new SelectListItem();
                item.Text = pg.Title;
                item.Value = pg.Id.ToString();
                contentPages.Add(item);
            }
            int selectedValue = 0;
            if (id.HasValue && id > 0)
            {
                ContentPageDomainModel page = _db.Pages.ById(id).FirstOrDefault().ToDomainModel();
                if (page != null)
                {
                    vm.Id = page.Id;
                    vm.MenuTitle = page.MenuTitle;
                    vm.PageName = page.PageName;
                    vm.Parent = page.Parent;
                    selectedValue = page.Parent ?? 0;
                    vm.Title = page.Title;
                    vm.HideFromMenu = page.HideFromMenu;
                    vm.Body = page.Body;
                }
            }
            contentPages.Insert(0, new SelectListItem() { Value = "0" });
            vm.pagesDropDown = new SelectList(contentPages, "Value", "Text", selectedValue);
            return vm;
        }

        private ContentPageDomainModel AddContentPage(ContentPageDomainModel page)
        {
            ContentPage content = _db.Add(page.ToEntity());
            _db.Save();
            return content.ToDomainModel();
        }

        private ContentPageDomainModel UpdateContentPage(ContentPageDomainModel page)
        {
            ContentPage content = _db.Update(page.ToEntity());
            _db.Save();
            return content.ToDomainModel();
        }
    }
}